<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="com.st.mall.user.model.UserModel"%>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/web/header.jsp"%>
<title>购物车</title>
</head>
<body>

	<div class="banner_x center">
		<a href="./index.jsp"><div class="logo2 fl"></div></a>
		<div class="wdgwc fl ml40">我的购物车</div>
		<div class="wxts fl ml20">温馨提示：产品是否购买成功，以最终下单为准哦，请尽快结算</div>
		<div class="dlzc fr">
			<ul>
				<li><a href="./index.jsp">首页</a></li>
				<li>|</li>
				<% Object obj = session.getAttribute("user");
						if(obj==null){%>
				<li><a href="./login.jsp">登录</a></li>
				<li>|</li>
				<li><a href="./reg.jsp">注册</a></li>
				<%  }else{ UserModel user = (UserModel)obj; %>
				<li><a href="javascript:dingdan()">我的订单[<%=user.getName()%>]
				</a></li>
				<li>|</li>
				<li><a href="javascript:toLogout()">注销</a></li>
				<%	}%>
			</ul>

		</div>
		<div class="clear"></div>
	</div>
	<div class="xiantiao"></div>
	<div class="gwcxqbj">
		<div class="gwcxd center">
			<div class="top2 center">
				<div class="sub_top fl">
					<input id="allchoose" type="checkbox" value="quanxuan"
						class="quanxuan" onclick="allchoose(this)" />全选
				</div>
				<div class="sub_top fl">商品名称</div>
				<div class="sub_top fl">单价</div>
				<div class="sub_top fl">数量</div>
				<div class="sub_top fl">小计/元</div>
				<div class="sub_top fr">操作</div>
				<div class="clear"></div>
			</div>
		</div>
		<div class="gwcxd center" id="hezi" ></div>
		<div class="jiesuandan mt20 center">
			<div class="tishi fl ml20">
				<ul>
					<li><a href="./list.jsp">继续购物</a></li>
					<li>|</li>
					<li><a href="javascript:delAll();">批量删除</a></li>
					<li>|</li>
					<li>共<span id='total'>0</span>件商品，已选择<span id="tchoose">0</span>件
					</li>
					<div class="clear"></div>
				</ul>
			</div>
			<div class="jiesuan fr">
				<div class="jiesuanjiage fl">
					合计（不含运费）：<span id="tmoney">0</span>元
				</div>
				<div class="jsanniu fr">
					<input class="jsan" type="submit" name="jiesuan" value="去结算" />
				</div>
				<div class="clear"></div>
			</div>
			<div class="clear"></div>
		</div>
	</div>
	<hr>
	<!-- footer -->
	<%@ include file="/web/front/title/end.jsp"%>
</body>

<script type="text/javascript">

init()
function init(){
	ajax({},'/scart/selectAll.do','json',function(data){
		if(data=="0"){
			layer.msg("请先登录！",{
				time : 1000
			},function(){
				toJsp("/web/front/login.jsp")
			});
			return
		}
		var h = ''
		var html1 = '';
		$.each(data,function(i,dom){
			var id = dom.id;
			html1 += '<div class="content2 center">'
			html1 += '<div class="sub_content fl "><input type="checkbox" value="'+ dom.proCode +'" name="quanxuan" class="quanxuan" onclick="tchoose()"/></div>'
			html1 += '<div class="sub_content fl"><img src="/image/'+dom.proUrl+'" style="width:100px;height:90px;"></div>'
			html1 += '<div class="sub_content fl ft20">'+ dom.proName +'</div>'
			html1 += '<div class="sub_content fl ">'+ dom.price +'</div>'
			html1 += '<div class="sub_content fl"><input class="shuliang" type="number" value="'+ dom.numb +'" step="1" min="1" onclick="numchange(this,\''+ dom.proCode +'\')"></div>'
			html1 += '<div class="sub_content fl"><span>'+ getTotal(dom.price,dom.numb) +'</span></div>'
			html1 += '<div class="sub_content fl"><a href="javascript:del();">×</a></div>'
			html1 += '<div class="clear"></div></div>'
		});
		$("#hezi").html(html1);
		$("#total").text(data.length);
	})
}
//总计
function getTotal(price,numb){
	return new Number(parseFloat(price)*parseFloat(numb)).toFixed(2);
}


//数量与总价的改变
function numchange(obj,code){
	var value = obj.value;
	ajax({proCode:code,numb:value},'/scart/change.do','json',function(data){
		if(data==1){
			obj.value=value;
			var price = $(obj).parent().prev().text();
			$(obj).parent().next().text(getTotal(price,value));
			tmoney()
		}
	})
}
//全选
var qx=document.getElementsByName('quanxuan');
function allchoose(obj){
	for (i in qx){
		qx[i].checked = obj.checked;
	}
	tchoose()
	tmoney()
}
//已选数量
function tchoose(){
	var cho = new Array();
	$("input[name='quanxuan']:checked").each(function(){
		cho.push($(this).val());
	})
	$("#tchoose").text(cho.length);
	tmoney()
} 


//总价
function tmoney(){
	var pri = 0;
	$("input[name='quanxuan']:checked").each(function(){
		var total = $(this).parent().next().next().next().next().next().text();
		pri = parseFloat(total)+parseFloat(pri);
	})
	$("#tmoney").text(new Number(parseFloat(pri)).toFixed(2));
}

//删除
function del(){
	layer.confirm("确定要将该式神移出购物车吗？",{icon: 1, title:'提示'},function(index){
		ajax({proCode:proCode},'/scart/del.do', 'text', function(data){
			if("1" == data){
				layer.msg("删除成功",refresh)
			}else{
				layer.msg("删除失败")
			}
		})
	})
}
//批量删除
function delAll(){
	var f = false;
	layer.confirm("确定要将这些式神移除吗？",{icon: 2, title:'提示'},function(index){
		$("input[name='quanxuan']:checked").each(function(){
			var code = $(this).val()
			ajax({proCode:code},'/scart/delAll.do', 'text', function(data){
				if("1" == data){
					f = true;
				}
			})
		})
		if(f==true){
			layer.msg("删除成功！",refresh)
		}
	})
}
function refresh(){
	toJsp('/web/front/cart.jsp')
}
//结算
$("input[name='jiesuan']").click(function(){
	
	ajax({},'/orderinfo/jiesuan.do','text',function(data){
		if(data=="A"){
			layer.msg("您还未添加默认地址，请前往添加", {
				time : 1000
			}, function() {
				toJsp("/web/front/myaddress.jsp")
			});
		}else{
			jiesuan(data);
		}
		
	})	
})
	function jiesuan(code){
		var qx = $("input[name='quanxuan']:checked")
		var t = true;
		qx.each(function(i,dom){
			var proCode = $(this).val();
			ajax({orderCode:code,proCode:proCode},'/orderpro/jiesuan.do','text',function(data){ 
				if(data==2) {
					t = false;
					return false;
				}	
			})
		});
		if(qx.length==0){
			layer.msg("请先选择要结算的式神！")
			return
		}
		if(t==true){
			layer.msg("结算成功，已提交订单", {
				time : 1000
			}, function() {
				toJsp("/web/front/order.jsp")
			});
		}else{
			layer.msg("结算失败");
		}
	}
//跳转到订单页面
function dingdan(){
	toJsp('/web/front/order.jsp')
}
//注销
function toLogout(){
	openConfirm(function(index){
		toJsp('/user/userOut.do')
	},"是否进行注销？");
}

</script>
</html>